

//Variaveis globais 
var actions =  new Action();
var formComponents = new Array();
/**
 * div que conterm a tabela com os componentes gerados dinamicamente 
 */
var formArea;

	
//********************fun��es utilit�rias*****************************

function goBack(){
	history.back();
}

function closeWindow(){
	//alert("shit");
	self.close();
}

/**
 * gera o xml que vai para o server
 */
function buildData(componentList, action, formId){
	var xmlData = "<?xml version=\"1.0\"?>";
	xmlData += "<ROOT>"; 
	
	xmlData += "<FORM value=\""+formId+"\">";
	xmlData += "</FORM>";
	
	xmlData += "<ACTION value=\""+action+"\">";
	xmlData += "</ACTION>";
	
	for ( var i = 0; i < componentList.length; i++) {
		var component = componentList[i];
		xmlData += "<COMPONENT>";
		
		xmlData += "<DATA id=\""+component.getId()+"\" value=\""+component.getValue()+"\" > </DATA>";
		
		xmlData += "</COMPONENT>";
	}
	xmlData += "</ROOT>";
	return xmlData;
}
	

function getById(id){
	return window.document.getElementById(id);
}


function changeComponent(id){

	//obt�m o componente 
	var component = eval("comp"+id);
	
	var xmlData = "<?xml version=\"1.0\"?>";
	
	xmlData += "<ROOT>";
	xmlData += "<FORM value=\""+FORM+"\"> </FORM>";
	xmlData += "<ACTION value=\"UPDATE\"></ACTION>";
	xmlData += "<CHECK_LIST_NAME value=\""+CHECK_LIST_NAME+"\"></CHECK_LIST_NAME>";
	xmlData += "<COMPONENT>";
	
	xmlData += "<DATA id=\""+component.getId()+"\" value=\""+component.getValue()+"\" > </DATA>";
	
	xmlData += "</COMPONENT>";
	xmlData += "</ROOT>";
	
    var conn = new ClientConnector();
	
	conn.setCallBack(function(code){
		eval(code);
	});
	
	conn.send(xmlData);
	
}

function focusComponent(id){

	//obt�m o componente 
	var component = eval("comp"+id);
	
	var xmlData = "<?xml version=\"1.0\"?>";
	
	xmlData += "<ROOT>";
	xmlData += "<FORM value=\""+FORM+"\"> </FORM>";
	xmlData += "<ACTION value=\"UPDATE\"></ACTION>";
	xmlData += "<CHECK_LIST_NAME value=\""+CHECK_LIST_NAME+"\"></CHECK_LIST_NAME>";
	xmlData += "<COMPONENT>";
	
	xmlData += "<DATA id=\""+component.getId()+"\" enabled=\"false\" > </DATA>";
	
	xmlData += "</COMPONENT>";
	xmlData += "</ROOT>";
	
    var conn = new ClientConnector();
	
	conn.setCallBack(function(code){
		eval(code);
	});
	
	conn.send(xmlData);
	
}

function blurComponent(id){

	//obt�m o componente 
	var component = eval("comp"+id);
	
	var xmlData = "<?xml version=\"1.0\"?>";
	
	xmlData += "<ROOT>";
	xmlData += "<FORM value=\""+FORM+"\"> </FORM>";
	xmlData += "<ACTION value=\"UPDATE\"></ACTION>";
	xmlData += "<CHECK_LIST_NAME value=\""+CHECK_LIST_NAME+"\"></CHECK_LIST_NAME>";
	xmlData += "<COMPONENT>";
	
	xmlData += "<DATA id=\""+component.getId()+"\" enabled=\"true\" > </DATA>";
	
	xmlData += "</COMPONENT>";
	xmlData += "</ROOT>";
	
    var conn = new ClientConnector();
	
	conn.setCallBack(function(code){
		eval(code);
	});
	
	conn.send(xmlData);
	
}

function changeInvalidComponent(id, list){
	list = eval(list);
	for ( var i = 0; i < list.length; i++) {
		var item = list[i];
		if(item.getId() == id){
			if(item.getValue() == ""){
				item.setInvalid(true);
			}else{
				item.setInvalid(false);
			};
		};
	};
}



//********************fim fun��es utilit�rias**********************

function User(name, login, type){
	this.name = name;
	this.login = login;
	this.type = type;
}


User.prototype = {};

User.prototype.contructor = User;

/**
 * objeto que representa a a��o enviada para o server
 */
function Action(){
	this.insert = "INSERT";
	this.update = "UPDATE";
	this.del = "DELETE";
	this.select = "SELECT";
	this.open = "OPEN";
}
Action.prototype = {};
Action.prototype.constructor=Action;


/**
 *  objeto que envia os dados para o sever
 * @returns
 */
function ClientConnector(){
	this.xmlHttp = new XMLHttpRequest();
	this.callBack = new Function();
	this.method = "POST";
}

ClientConnector.prototype = {};

ClientConnector.prototype.constructor = ClientConnector;

ClientConnector.prototype.send = function(data){
	
	var url = "http://" + window.location.host + "/baker-street-web/MainServlet";
	//this.xmlHttp.setRequestHeader('Content-type',"charset=utf-8");
	this.xmlHttp.open(this.method, url, false, null, null);
	this.xmlHttp.send(data);
	
	
	this.callBack.call(this,this.xmlHttp.responseText);
};

ClientConnector.prototype.setMethod = function(method){
	this.method = method;
};

ClientConnector.prototype.setCallBack = function(callBack){
	this.callBack = callBack;
};
//********************COMPONENT************************************

function Component(id,parentElem){
	this.id = id;
	//vari�vel que representar� a tag html do componente 
	//altera��es visuais no componente ser�o efetuadas atrav�s dela 
	this.htmlElement = null;
	this.parentElem = getById(parentElem);
	this.height = null;
	this.width = null;
	this.value = null;
	this.ignoreDisable = false;
	this.description = null;
	this.invalidColor = "2px solid #E6E600";
	
}

Component.prototype = {};

Component.prototype.constructor = Component;

Component.prototype.getId = function(){
	return this.id;
};

Component.prototype.getValue = function(){
	return this.htmlElement.value;
};

Component.prototype.setValue = function(value){
	this.htmlElement.value = value;
};

//shortHand para a cria��o de elemento HTML 
Component.prototype.crEl = function(tagName){
	return window.document.createElement(tagName);
};

Component.prototype.render = function(){
	this.htmlElement.id = this.id;
	this.parentElem.appendChild(this.htmlElement);
	formComponents.push(this);
	this.htmlElement.onchange = new Function("changeComponent(\""+this.id+"\");");
	this.htmlElement.onfocus = new Function("focusComponent(\""+this.id+"\");");
	this.htmlElement.onblur = new Function("blurComponent(\""+this.id+"\");");
};

Component.prototype.setEnabled = function(enabled){
	//var log = getById("logger");
	if(this.isIgnoreDisable()){
		this.setIgnoreDisable(false);
	}else{
		if(enabled == true){
			this.htmlElement.disabled = "";
			//log.innerHTML = log.innerHTML + "passou no habilitar "+enabled;
		}else{
			this.htmlElement.disabled = "disabled";
			//log.innerHTML = log.innerHTML + "passou no desabilitar "+enabled;
		}
	}
};

Component.prototype.isIgnoreDisable = function(){
	return this.ignoreDisable;
};

Component.prototype.setIgnoreDisable = function(ignore){
	this.ignoreDisable = ignore;
};

/**
 * so aplica depois do componente renderizado
 * @param height
 */
Component.prototype.setHeight = function(height){
	this.height = height;
	this.htmlElement.style.height = height+"px";
};

/**
 * s� aplica depois do componente renderizado
 * @param width
 */
Component.prototype.setWidth = function(width){
	this.width = width;
	this.htmlElement.style.width = width+"px";
};


Component.prototype.setDescription = function(description){
	this.description = description;
};

Component.prototype.getDescription = function(){
	return this.description;
};

Component.prototype.setInvalid = function(invalid){
	if(invalid == true){
		this.htmlElement.style.border = this.invalidColor;
	}else{
		this.htmlElement.style.border = "";
	}
};

Component.prototype.setClassName = function(name){
	this.htmlElement.className=name;
};


//***************************EDITBOX*****************************

function EditBox(id, parentElem){
	Component.prototype.constructor.call(this,id,parentElem);
};

EditBox.prototype= new Component();

EditBox.prototype.constructor = EditBox;

EditBox.prototype.render = function(){
	this.htmlElement = this.crEl("input");
	this.htmlElement.className = "data-edit";
	this.htmlElement.type="text";
	Component.prototype.render.call(this);
};

//*********************CHECKBOX***********************************

function CheckBox(id, parentElem){
	Component.prototype.constructor.call(this,id,parentElem);
};

CheckBox.prototype= new Component();

CheckBox.prototype.constructor = CheckBox;

CheckBox.prototype.render = function(){	
	this.htmlElement = this.crEl("input");
	this.htmlElement.type="checkbox";
	Component.prototype.render.call(this);
};

CheckBox.prototype.getValue = function(){
	return this.htmlElement.checked;
};

CheckBox.prototype.setValue = function(checked){
	if(checked != this.htmlElement.checked){
		this.htmlElement.checked = checked;
	}
};


//**********************COMBOBOX*************************

function ComboBox(id, parentElem){
	Component.prototype.constructor.call(this,id,parentElem);
	
	this.options = new Array();
};

ComboBox.prototype= new Component();

ComboBox.prototype.constructor = ComboBox;

ComboBox.prototype.render = function(){	
	this.htmlElement = this.crEl("select");
	this.htmlElement.type="checkbox";
	this.htmlElement.className = "data-combo";
	Component.prototype.render.call(this);
	
	for(var i = 0; i < this.options.length;i++){
		var opt = this.crEl("option");
		opt.innerHTML = this.options[i];
		this.htmlElement.appendChild(opt);
	}
};

ComboBox.prototype.addOption = function(option){
	this.options.push(option);
};


ComboBox.prototype.setValue = function(value){
	var childs = this.htmlElement.childNodes;
	for ( var i = 0; i < childs.length; i++) {
		var child = childs[i];
		
		if(child.selected == true){
			child.selected = false;
		}
		
		if(value == child.value){
			child.selected = true;
		}
	}
};




//**********************TEXTAREA*******************************


function TextArea(id, parentElem){
	Component.prototype.constructor.call(this,id,parentElem);
};

TextArea.prototype= new Component();

TextArea.prototype.constructor = TextArea;

TextArea.prototype.render = function(){	
	this.htmlElement = this.crEl("textarea");
	this.htmlElement.className = "data-textarea";
	Component.prototype.render.call(this);
};

function Label(id, parentElem){
	Component.prototype.constructor.call(this,id,parentElem);
};

Label.prototype= new Component();

Label.prototype.constructor = Label;

Label.prototype.render = function(){	
	this.htmlElement = this.crEl("label");
	Component.prototype.render.call(this);
};

Label.prototype.setValue = function(value){
	this.htmlElement.innerHTML = value;
	this.value = value;
};

